using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DailyTransaction
{
    public partial class frmMaint_AccountCode : Form
    {
        private BAO bao = new BAO();
        private bool edit = false;
        private bool read_only = false;

        public frmMaint_AccountCode()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void set_Read_Only_To_Component(bool value)
        {
            cmbPayDesc.Enabled = !value;
            txtCompName.ReadOnly = value;
            txtDebit.ReadOnly = value;
            txtDispoAcct.ReadOnly = value;
            txtDrugAcct.ReadOnly = value;
            txtMedAcct.ReadOnly = value;
            txtSrvTaxAcct.ReadOnly = value;
            txtT0Code.ReadOnly = value;
            txtT4Code.ReadOnly = value;
            btnSave.Enabled = !value;
        }

        private void set_Blank_To_Component()
        {
            cmbPayType.Text = "";
            cmbPayDesc.Text = "";
            txtCompName.Text = "";
            txtDebit.Text = "";
            txtDispoAcct.Text = "";
            txtDrugAcct.Text = "";
            txtMedAcct.Text = "";
            txtSrvTaxAcct.Text = "";
            txtT0Code.Text = "";
            txtT4Code.Text = "";
        }

        private void frmMaint_AccountCode_Load(object sender, EventArgs e)
        {
            read_only = true;
            cmbPayType.DisplayMember = "kind_pment";
            cmbPayType.ValueMember = cmbPayType.DisplayMember;
            cmbPayType.DataSource = bao.get_Paymode_revised();
            set_Read_Only_To_Component(read_only);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            read_only = false;
            edit = false;
            set_Blank_To_Component();
            set_Read_Only_To_Component(read_only);
            cmbPayDesc.DisplayMember = "kind_pment_desc";
            cmbPayDesc.ValueMember = cmbPayDesc.DisplayMember;
            cmbPayDesc.DataSource = bao.get_Pment_Desc();
            read_only = true;
            //if (!edit) cmbPayType.Enabled = true;
        }

        private void cmbPayType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = (ComboBox) sender;
            System.Data.DataRowView dtv = (System.Data.DataRowView)cmbPayType.SelectedItem;
            string text = dtv.Row[0].ToString();
            if (text.CompareTo("") == 0)
            {
                set_Blank_To_Component();
                return;
            }
            DataTable tbl = bao.get_Ab_Account_Code(text);
            //MessageBox.Show(tbl.Rows[0]["ACC_NAME"].ToString());
            set_Value_To_Component(tbl.Rows[0]["ACC_NAME"].ToString(),
                tbl.Rows[0]["ACC_DEB"].ToString(), tbl.Rows[0]["ACC_CRE_MED"].ToString(),
                tbl.Rows[0]["ACC_CRE_SERV"].ToString(), tbl.Rows[0]["ACC_CRE_DRUG"].ToString(),
                tbl.Rows[0]["ACC_CRE_DISP"].ToString(), tbl.Rows[0]["T0_code"].ToString(),
                tbl.Rows[0]["T4_code"].ToString(), tbl.Rows[0]["KIND_PMENT_DESC"].ToString());
            //if (edit) cmbPayType.Enabled = false;
        }

        private void set_Value_To_Component(string acc_name, string acc_deb, string acc_cre_med, string acc_cre_serv,
            string acc_cre_drug, string acc_cre_disp, string t0_code, string t4_code, string kind_pment_desc)
        {
            txtCompName.Text = acc_name;
            txtDebit.Text = acc_deb;
            txtDispoAcct.Text = acc_cre_disp;
            txtDrugAcct.Text = acc_cre_drug;
            txtMedAcct.Text = acc_cre_med;
            txtSrvTaxAcct.Text = acc_cre_serv;
            txtT0Code.Text = t0_code;
            txtT4Code.Text = t4_code;
            cmbPayDesc.Text = kind_pment_desc;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string kind_pment = cmbPayType.Text;
            string acc_name = txtCompName.Text;
            string acc_deb = txtDebit.Text;
            string acc_cre_med = txtMedAcct.Text;
            string acc_cre_drug = txtDrugAcct.Text;
            string acc_cre_disp = txtDispoAcct.Text;
            string acc_cre_serv = txtSrvTaxAcct.Text;
            string t0_code = txtT0Code.Text;
            string t4_code = txtT4Code.Text;
            string kind_pment_desc = cmbPayDesc.Text;
            if (kind_pment_desc.CompareTo("") == 0)
            {
                MessageBox.Show("Pls select Payment Desc.");
                cmbPayDesc.Focus();
                return;
            }
            if (edit == true)
            {
                if (bao.usp_Update_AB_ACCOUNT_CODE(kind_pment, acc_deb, acc_name, acc_cre_med,
                    acc_cre_serv, acc_cre_drug, acc_cre_disp, t0_code, t4_code, kind_pment_desc)!=0)
                    MessageBox.Show("Success Updated!");
            }
            else
            {
                if (bao.usp_Insert_AB_ACCOUNT_CODE(kind_pment, acc_deb, acc_name, acc_cre_med,
                    acc_cre_serv, acc_cre_drug, acc_cre_disp, t0_code, t4_code, kind_pment_desc)!=0)
                    MessageBox.Show("Success Added!");
            }
            if (read_only == false)
            {
                read_only = true;
                set_Read_Only_To_Component(read_only);
                btnSave.Enabled = false;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            edit = true;
            cmbPayDesc.DisplayMember = "kind_pment_desc";
            cmbPayDesc.ValueMember = cmbPayDesc.DisplayMember;
            cmbPayDesc.DataSource = bao.get_Pment_Desc();
            if (read_only)
            {
                read_only = false;
                set_Read_Only_To_Component(read_only);
            }
            if (cmbPayType.Text.CompareTo("") == 0)
            {
                MessageBox.Show("Select account code to modify");
                cmbPayType.Focus();
                return;
            }
            if (cmbPayDesc.Text.CompareTo("") == 0)
            {
                MessageBox.Show("Select payment desc.");
                cmbPayDesc.FindForm();
                return;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            read_only = true;
            if (cmbPayType.Text.CompareTo("") == 0)
            {
                MessageBox.Show("Pls select an account code to delete!");
                cmbPayType.Focus();
                return;
            }
            DialogResult rt = MessageBox.Show("Are you sure!!!", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rt == DialogResult.Yes)
            {
                bao.usp_Delete_AB_ACCOUNT_CODE(cmbPayType.Text);
                MessageBox.Show("Record deleted!");
            }
            else return;
            set_Blank_To_Component();
            set_Read_Only_To_Component(read_only);
        }
    }
}